var _Target = require('./targetaction.js');

var _ButtonIds = require('event/buttonid.js');
var BUTTON_DOWN = _ButtonIds.BUTTON_DOWN;
var BUTTON_UP = _ButtonIds.BUTTON_UP;
var BUTTON_MOVE = _ButtonIds.BUTTON_MOVE;
var BUTTON_CANCEL = _ButtonIds.BUTTON_CANCEL;

// ==========================
// SwipeTrigger
// ==========================
function SwipeTrigger(triggerclick) {
    _Target.onCreate(this);
    this.mTouchX = 0;
    this.mTriggerclick = triggerclick || false;
}
_Target.asAction(SwipeTrigger);
SwipeTrigger.prototype.onTouch = function (event) {
    switch (event.state) {
        case BUTTON_DOWN:
            this.startMove(event.vector);
            break;

        case BUTTON_UP:
        case BUTTON_CANCEL:
            this.endMoving(event.vector);
            break;

        case BUTTON_MOVE:
            this.keepMoving(event.vector);
            break;
    }
    return true;
};
SwipeTrigger.prototype.startMove = function (vector) {
    this.mTouchX = vector[0];
};
SwipeTrigger.prototype.keepMoving = function (vector) {
    if (Math.abs(vector[0] - this.mTouchX) > 60) {
        this.fire('finish');
        this.off('finish');
    }
};
SwipeTrigger.prototype.endMoving = function (vector) {
    if (this.mTriggerclick || Math.abs(vector[0] - this.mTouchX) > 60) {
        this.fire('finish');
        this.off('finish');
    }
};
SwipeTrigger.prototype.toString = function () {
    return '[object SwipeTrigger]';
};

module.exports = SwipeTrigger;